﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <sc-export
                    :queryType="`pos-sale/store-inventory-export`"
                    :queryList="queryList"
                    :fileName="'门店日报表'"
                    :isTest="true"
                    showText="导出"
                />
                <el-button type="primary" icon="el-icon-plus" @click="openAdd" :loading="openAddState">生成盘点单</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="openStockGoodsAdd" :loading="openStockGoodsAddState">商品同步</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="openStockAdd" :loading="openStockAddState">库存同步</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px">
                    <el-form-item label="营业时间" label-width="100px">
                        <el-date-picker
                            style="width: 300px"
                            v-model="queryList.consumeTimes"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            :default-time="defaultTime"
                        />
                    </el-form-item>
                    <el-form-item label="点单机构" label-width="100px">
                        <el-select
                            placeholder="---请选择点单机构---"
                            v-model="queryList.orgCode"
                            style="width: 300px"
                            collapse-tags
                            collapse-tags-tooltip
                        >
                            <el-option
                                v-for="item in posStoreOrgs"
                                :key="item.posStoreCode"
                                :label="item.posStoreName"
                                :value="item.posStoreCode"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="点单商品编码" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.goodsCode"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="点单商品名称" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.goodsName"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="点单商品规格" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.standard"
                            style="width: 300px"
                        />
                    </el-form-item>
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'pos-sale/store-inventory'"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="posMaterialCompareId"
                key="posMaterialCompareId"
                stripe
                show-summary
                remoteSummary
            >
                <!-- <el-table-column label="操作" fixed="right" align="center" min-width="180">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                :disabled="!editState"
                                v-show="isShow"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        v-show="isShow"
                                        :disabled="!deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>-->
            </scTable>
        </el-main>
    </el-container>
</template>

<script>
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "storeInventory",
    components: {
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["itemCode"],
            columnList: [
                {
                    prop: "goodsCode",
                    label: "点单商品编号",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "goodsName",
                    label: "点单商品名称",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "materialCode",
                    label: "物料编号",
                    width: "240",
                    sortable: false,
                },
                {
                    prop: "materialName",
                    label: "物料名称",
                    width: "240",
                    sortable: false,
                },
                {
                    prop: "materialSpecs",
                    label: "物料规格",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "unitName",
                    label: "物料单位",
                    width: "110",
                    sortable: false,
                },
                {
                    prop: "startInventory",
                    label: "期初",
                    width: "160",
                    sortable: true,
                },
                {
                    prop: "barInventory",
                    label: "吧台库存",
                    width: "160",
                    sortable: true,
                },
                {
                    prop: "todayPickUp",
                    label: "拾遗",
                    width: "100",
                    sortable: false,
                },
                {
                    prop: "saleCount",
                    label: "售卖",
                    width: "110",
                    sortable: false,
                },
                {
                    prop: "changeCount",
                    label: "置换",
                    width: "120",
                    sortable: false,
                },
                {
                    prop: "saleInventory",
                    label: "销售库存",
                    width: "110",
                    sortable: false,
                },
                {
                    prop: "inStorageCount",
                    label: "寄存",
                    width: "120",
                    sortable: false,
                },
                {
                    prop: "outStorageCount",
                    label: "取出",
                    width: "110",
                    sortable: false,
                },
                {
                    prop: "todayInventory",
                    label: "本日实盘",
                    width: "120",
                    sortable: false,
                },
                {
                    prop: "inStockQty",
                    label: "供应链入库数量",
                    width: "120",
                    sortable: false,
                },
                {
                    prop: "outStockQty",
                    label: "供应链出库数量",
                    width: "120",
                    sortable: false,
                },
                {
                    prop: "totalInventory",
                    label: "总库存",
                    width: "110",
                    sortable: false,
                },
                {
                    prop: "inventoryDif",
                    label: "盘点差异",
                    width: "120",
                    sortable: false,
                },
            ],
            OrderList: { itemCode: "Asc" },
            queryList: {
                materialTypeIds: [],
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            posStoreOrgs:[],
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            goodTypeTree: [],
            isShow: true, //是否显示
            defaultTime: [
                new Date(2000, 1, 1, 12, 0, 0),
                new Date(2000, 2, 1, 12, 0, 0),
            ],
            openAddState:false,
            openStockAddState:false,
            openStockGoodsAddState:false,
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {orgCode:this.posStoreOrgs[0].posStoreCode};
        },
        //生成盘点单
        async openAdd() {
            this.openAddState=true;
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "store-inventory/create",
                    {}
                );
                if (returnValue.status == 200) {
                    this.openAddState=false;
                    this.$router.push({
                        path: "/posManage/storeInventoryStock",
                        query: {},
                    });
                    this.$message.success("操作成功");
                } else {
                    this.openAddState=false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.openAddState=false;
                this.loading = false;
                return false;
            }
        },
        //手动同步库存
        async openStockAdd() {
            this.openStockAddState=true;
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "store/create-pos-stocks",
                    {}
                );
                if (returnValue.status == 200) {
                    this.openStockAddState=false;
                    await this.opBindData();
                    this.$message.success("操作成功");
                } else {
                    this.openStockAddState=false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.openStockAddState=false;
                this.loading = false;
                return false;
            }
        },
        //手动同步商品
        async openStockGoodsAdd(){
            this.openStockGoodsAddState=true;
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "store/create-pos-stock-goods",
                    {}
                );
                if (returnValue.status == 200) {
                    this.openStockGoodsAddState=false;
                    await this.opBindData();
                    this.$message.success("操作成功");
                } else {
                    this.openStockGoodsAddState=false;
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.openStockGoodsAddState=false;
                this.loading = false;
                return false;
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "material-type/tree",
                    { status: 1 }
                );
                if (returnValue.status == 200) {
                    this.materialTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/dic-options",
                    { ParentBaseDictionaryCodes: ["001", "002"] }
                );
                if (returnValue.status == 200) {
                    this.optionsBrand = returnValue.response.find(
                        (p) => p.code == "001"
                    ).optionsDetails;
                    this.optionsUnit = returnValue.response.find(
                        (p) => p.code == "002"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            // try {
            //     var returnValue = await this.$API.auth.PostProcessApi.post(
            //         "pos-sale/org-type",
            //         {}
            //     );
            //     if (returnValue.status == 200) {
            //         this.goodTypeTree = returnValue.response;
            //     } else {
            //         this.$message.error("操作失败:" + returnValue.msg);
            //     }
            // } catch (error) {
            //     this.loading = false;
            //     return false;
            // }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "store/pos-org-cnfigs",
                    {}
                );
                if (returnValue.status == 200) {
                    this.posStoreOrgs = returnValue.response;
                    this.queryList.orgCode=this.posStoreOrgs[0].posStoreCode;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        formatDate(date, type = 0) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, "0");
            const day = String(date.getDate()).padStart(2, "0");
            const hours = String(date.getHours()).padStart(2, "0");
            const minutes = String(date.getMinutes()).padStart(2, "0");
            const seconds = String(date.getSeconds()).padStart(2, "0");

            return type == 0
                ? `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
                : `${year}-${month}-${day}`;
        },
    },
    async mounted() {
        var orgType = tool.data.get("USER_INFO").AccountType;
        if (orgType == 1) {
            this.isShow = true;
        }
        // 获取当前日期
        const now = new Date();
        // 创建前一天的日期对象
        let previousDay = new Date(now);
        // 判断当前时间是否大于12点
        const currentHour = previousDay.getHours();
        if (currentHour < 12) {
            previousDay.setDate(now.getDate() - 1); // 设置为前一天
        }
        // 将时间设置为 12:00 PM
        previousDay.setHours(12, 0, 0, 0); // 设置时、分、秒、毫秒为 12:00:00.000
        now.setHours(12, 0, 0, 0); // 设置时、分、秒、毫秒为 12:00:00.000
        this.queryList.consumeTimes = [
            this.formatDate(previousDay),
            this.formatDate(now),
        ];
        console.log("consumeTimes", this.queryList);
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
